#include "ctrain.h"

ctrain::ctrain(int lenght){
	for (int i=0;i<3;i++)
		orail[i].load(i);
		
	railroad.resize(1);
	railroad[0].x=-100;
	railroad[0].y=0;
	railroad[0].type=1;
	railroad[0].rot=0;
	railroad[0].index=0;

	for (int i=0;i<lenght*(WAG_LENGHT+1)/RAIL_LENGHT+1+(WAG_LENGHT-1)/RAIL_LENGHT+1;i++){
		railroad.push_back(orail[0].next(1,railroad.back()));
		railroad[i].next= & railroad.back();
	}
	
	wagons.resize(lenght);
	for (int i=0;i<lenght;i++){
		if (i==0)
			wagons[i]=new obj_wagon_locomotive;
		else
			wagons[i]=new obj_wagon_flat;
		debug("Vyrabam vagon na kolajach %f %f\n",(lenght-i-1)*(WAG_LENGHT+1)/RAIL_LENGHT,(lenght-i-1)*(WAG_LENGHT+1)/RAIL_LENGHT+(WAG_LENGHT-1)/RAIL_LENGHT+1);
		wagons[i]->rrfrom=& railroad[0];
		wagons[i]->rrto= wagons[i]->rrfrom->next;
		wagons[i]->rprogress=(lenght-i-1)*(WAG_LENGHT+1)/(float)RAIL_LENGHT;
		wagons[i]->rffrom=& railroad[0];
		wagons[i]->rfto= wagons[i]->rffrom->next;
		wagons[i]->fprogress=((lenght-i-1)*(WAG_LENGHT+1)+(WAG_LENGHT-1))/((float) RAIL_LENGHT);
	}
	
	addrail_curtype=1;
	addrail_same=0;
}

void ctrain::draw(){
	FOR (i,railroad.size())
		orail[railroad[i].type].draw_rail(railroad[i]);
	FOR (i, wagons.size())
		wagons[i]->draw_wagon();
	FOR (i, obstacles.size())
		obstacles[i].draw();
}

void ctrain::update(){
	for (int i=0;i<wagons.size();i++)
		wagons[i]->update();
	x=(wagons[0]->x+wagons[wagons.size()-1]->x)/2;
	y=(wagons[0]->y+wagons[wagons.size()-1]->y)/2;

	while (square(railroad.front().x-x)+square(railroad.front().y-y)>square(CALC_DISTANCE))
		railroad.pop_front();
	while (square(obstacles.front().x-x)+square(obstacles.front().y-y)>square(CALC_DISTANCE))
		obstacles.pop_front();
}


int ctrain::addrail(){
	if (square(x-railroad.back().x)+square(y-railroad.back().y)>square(CALC_DISTANCE))
		return 0;
	if (addrail_same>0){
		addrail_same--;
		railroad.push_back(orail[0].next(addrail_curtype,railroad.back()));
		railroad[railroad.size()-2].next= & railroad.back();
		FOR(i,1000)
			addobstacles();
		return 1;
	}
	
	if (addrail_curtype==1){
		addrail_curtype=2*(rand()%2);
		addrail_same=(rand()%80+20)/100.*M_PI/2/(RAIL_ANGLE);	
		return addrail();
	}
	else{
		addrail_curtype=1;//-(addrail_curtype-1)*(rand()%2);
		addrail_same=(rand()%50+50)/100.*50/(RAIL_LENGHT);
		return addrail();
	}
}

void ctrain::addobstacles (){
	if ((rand()%1000000)/1000000.>RAIL_LENGHT/2000.)
		return;
	obstacles.resize(obstacles.size()+1);
	obstacles.back().load(rand()%OBSTACLE_TYPE_NUM);
	obs_dst=(rand()%1001-500)/500.;
	obs_dst*=abs(obs_dst)*50;
	if (abs(obs_dst)<obstacles.back().r+RAIL_WIDTH){
		obstacles.pop_back();
		return;
	}

	obstacles.back().x=railroad.back().x - obs_dst*sin(railroad.back().rot);
	obstacles.back().y=railroad.back().y + obs_dst*cos(railroad.back().rot);
	obstacles.back().rot=rand()%((int)(2*M_PI*1000))/1000.;
	

	FOR(i,obstacles.size()-1)
		if (square(obstacles[i].x-obstacles.back().x)+square(obstacles[i].y-obstacles.back().y)<square(obstacles[i].r+obstacles.back().r)){
			obstacles.pop_back();
			return;
		}
	obstacles.back().calc_col_verts();
}
